package cc.linker.steplink.crm.merchant.controller;

import cc.linker.steplink.crm.commons.http.HttpService;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.commons.model.SystemProperties;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchanth5.service.LiveGoodsService;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/liveGoods")
public class LiveGoodsController {

    @Autowired
    private LiveGoodsService liveGoodsService;

    @Autowired
    private SystemProperties systemProperties;
    @Autowired
    private UserInfoUtil userInfoUtil;

    @Autowired
    private HttpService httpService;

    @Value("${system.scrm.scrmWebUrl}")
    private String scrmWebUrl;

    @Value("${casclient.clientHost}")
    private String clientHost;

    /**
     * 商品列表
     *
     * @param : pageIndex 页码
     * @param : pageSize  页面大小
     * @param : orgId  机构id
     * @param : goodsType 全部、分销、联盟商品
     * @param : type 分为全部、平台商品、知识商品-图文、知识商品-音频、知识商品-视频、知识商品-专栏
     */
    @PostMapping("/getGoodslist")
    public Object getGoodsList(@RequestBody Map<String, Object> params) {
        PageInfo list =  liveGoodsService.getLiveGoodsList(params);
//        Map map = new HashMap();
//        String goodsIds = liveGoodsService.getDeleteIds(params);
//        if(goodsIds==null){
//            goodsIds="";
//        }
//        map.put("dataList",list);
//        map.put("goodsIds",goodsIds);
        return list;
    }

    /**
     * 商品列表
     *
     * @param : goodsIds 商品id
     */
    @PostMapping("/getDeleteIds")
    public Object getDeleteIds(@RequestBody Map<String, Object> params) {
        Map map = new HashMap();
        String goodsIds = liveGoodsService.getDeleteIds(params);
        if(goodsIds==null){
            goodsIds="";
        }
        map.put("goodsIds",goodsIds);
        return map;
    }


    /**
     * 机构详情
     *
     */
    @PostMapping("/getOrgInfo")
    public Object getOrgInfo(@RequestBody Map<String, Object> params) {
        return liveGoodsService.getOrgInfo(params);
    }

    /**
     * 机构列表
     *
     */
    @PostMapping("/getOrgList")
    public Object getOrgList() {
        return liveGoodsService.getOrgList();
    }

    /**
     * 新增直播间
     *
     * @param : anchorName 主播名称
     * @param : iconUrl  头像
     * @param : studioId  直播间Id
     * @param : studioName 直播间名称
     * @param : watchLiveUrl 观看url
     * @param : orgId 机构id
     */
    @PostMapping("/addStudio")
    public Object addStudio(@RequestBody Map<String, Object> params) {
        String studioId = "";
        String orgId = "";
        if(params.get("studioId")!=null && params.get("orgId")!=null){
            studioId = params.get("studioId").toString();
            orgId = params.get("orgId").toString();
        }else {
            throw GlobalException.build("缺少参数！");
        }
        liveGoodsService.addStudio(params);
        return  liveGoodsService.getEndUrl(studioId,orgId,clientHost,scrmWebUrl);
    }

    /**
     * 编辑直播间
     *
     * @param : anchorName 主播名称
     * @param : iconUrl  头像
     * @param : studioId  直播间Id
     * @param : studioName 直播间名称
     * @param : watchLiveUrl 观看url
     * @param : orgId 机构id
     */
    @PostMapping("/updateStudio")
    public Object updateStudio(@RequestBody Map<String, Object> params) {
        String studioId = "";
        String orgId = "";
        if(params.get("studioId")!=null && params.get("orgId")!=null){
            studioId = params.get("studioId").toString();
            orgId = params.get("orgId").toString();
        }else {
            throw GlobalException.build("缺少参数！");
        }
        liveGoodsService.updateStudio(params);
        return  liveGoodsService.getEndUrl(studioId,orgId,clientHost,scrmWebUrl);
    }

    /**
     * 删除直播间
     *
     * @param : studioId  直播间Id
     */
    @PostMapping("/deleteStudio")
    public void deleteStudio(@RequestBody Map<String, Object> params) {
        liveGoodsService.deleteStudio(params);
    }

    /**
     * 根据sh5获取用户信息
     *
     * customerId
     *
     * @param : studioId  直播间Id
     */
    @PostMapping("/getuserInfo")
    public Object getuserInfo(String customerId){
        return liveGoodsService.getuserInfo(customerId);
    }

    /**
     * 获取链接地址
     *
     */
    @PostMapping("/getUrlList")
    public Object getUrlList(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return liveGoodsService.getUrlList(userinfo.getOrgId());
    }


    /**
     * 创建直播间
     *
     */
    @PostMapping("/saveThirdAccount")
    public Object saveThirdAccount(@RequestBody Map<String, Object> params) {
        String url = systemProperties.getVideoUrl() + "user/saveThirdAccount";
        try {
            String result;
            result = httpService.doPost(url, params);
            //{"code":"200","msg":"创建账号成功！","adminUrl":"http://hdadmin.linker.cc/videoLivevAdmin/","account":"1861111221","password":"123456","downImageUrl":""}
            if(StringUtils.isEmpty(result)){
                throw GlobalException.build("云观接口调用异常");
            }
            return result;
        } catch (Exception e) {
            throw GlobalException.build("云观接口调用异常");
        }
    }

}
